package cn.htsyb.synergy.utils;

import java.io.*;
import java.util.zip.GZIPOutputStream;

public class GZipUtil {
    /**
     * 数据压缩
     *
     * @param is
     * @param os
     * @throws Exception
     */
    public static void gzipCompres(InputStream is, OutputStream os) throws IOException {
        int buffSize = 1024;
        GZIPOutputStream gos = new GZIPOutputStream(os);
        int count;
        byte[] data = new byte[buffSize];
        while ((count = is.read(data, 0, buffSize)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.flush();
        gos.close();
    }

    /**
     * 文件压缩
     *
     * @param srcFile
     * @param tagerFile
     * @throws Exception
     */
    public static void compresFile(File srcFile, File tagerFile) throws Exception {
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(tagerFile);
        gzipCompres(fis, fos);
        fis.close();
        fos.flush();
        fos.close();
    }
}
